<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/operatehistory.jsp"%>
<%@ include file="/common/user.jsp"%>
<%!
	public List<Map> completePatientInfo(List<Map> list, Integer hospital, List<Integer> visitPatientUidList) {
		String uids = "-1";
		for (Map map:list) {
			uids += "," + Tools.getDesc(map.get("patient_uid"), "-1");
		}
		List<User> users = QBuilder.db("basedb").select().from("User").where(new Where("status= ", STATUS_ENABLE).and("hospital=", hospital).and("id in ("+ uids +")")).query().getResultObj();
		List<UserProfile> userProfiles = QBuilder.db("basedb").select().from("UserProfile").where(new Where("status= ", STATUS_ENABLE).and("hospital=", hospital).and("SELF=", SELF).and("uid in ("+ uids +")")).query().getResultObj();
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			String srcUid = Tools.getDesc(map.get("patient_uid"), "-1");
			map.put("patient_uid", Tools.getInt(srcUid));
			map.put("visiting", 0);
			if (visitPatientUidList.contains(Tools.getInt(srcUid))) {
				map.put("visiting", 1);
			}
			for (int j = 0; j < users.size(); j++) {
				User user = users.get(j);
				String id = user.getId() + "";
				if (srcUid.equals(id)) {
					map.put("avatar", getDefaultAvatar(user));
					map.put("type", Tools.getDesc(user.getType() + "", ""));
					map.put("name", !isEmpty(user.getName())?user.getName():!isEmpty(user.getNickName())?user.getNickName():user.getUserName());
					users.remove(user);
					break;
				}
			}
			for (int j = 0; j < userProfiles.size(); j++) {
				UserProfile userProfile = userProfiles.get(j);
				String uid = userProfile.getUid() + "";
				if (srcUid.equals(uid)) {
					Long birth = Tools.getLong(userProfile.getBirthday() + "", -1L);
					map.put("birthday", birth < 0 ? "" : DateUtil.formatDate(new Date(birth)));
					map.put("gender", Tools.getDesc(userProfile.getGender() + "", ""));
					map.put("self", Tools.getDesc(userProfile.getSelf() + "", "0"));
					map.put("age", Tools.getDesc(userProfile.getAge() + "", ""));
					map.put("phone", Tools.getDesc(userProfile.getPhone() + "", ""));
					map.put("card_type", Tools.getDesc(userProfile.getCardType() + "", ""));
					map.put("medical_type", Tools.getDesc(userProfile.getMedicalType() + "", ""));
					map.put("json", userProfile.getJson());
					userProfiles.remove(userProfile);
					break;
				}
			}
		}
		return list;
	}
%>
<%
	Integer doctorUid = token.getUid();
	List<Map> list = QBuilder.db("basedb").select("new map(o.uid as patient_uid)").from("OperateHistory as o").where(new Where("o.hospital=", hospital).and("o.category=", SECTION_DOCTOR).and("o.operate=", ATTENTION).and("o.status=", STATUS_ENABLE).and("o.oid=", doctorUid)).query().getResultObj();
	List<Map> attentionList = QBuilder.db("basedb").select("new map(o.oid as patient_uid)").from("OperateHistory as o").where(new Where("o.hospital=", hospital).and("o.category=", SECTION_PATIENT).and("o.operate=", ATTENTION).and("o.status=", STATUS_ENABLE).and("o.uid=", doctorUid)).query().getResultObj();
	List<Integer> visitPatientUidList = QBuilder.db("basedb").select("distinct patientUid").from("Visit as v").where(new Where("v.hospital=", hospital).and("v.status=", STATUS_ENABLE).and("v.doctorUid=", doctorUid)).query().getResultObj();
	Map result = new HashMap();
	result.put("all", completePatientInfo(list, hospital, visitPatientUidList));
	result.put("attention", completePatientInfo(attentionList, hospital, visitPatientUidList));
	msg.put("data", result);
	Tools.encryptPrint(out, msg);
%>
